home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Inside Mac Games Volume 4 #9
/
IMG 40 Sep 1996.iso
/
More Goodies
/
More for Your Game
/
Sprite Fight 2002
/
SpriteTool#3 Folder
/
SpriteFlipNMask.c
< prev
Wrap
C/C++ Source or Header
|
1996-07-18
|
21KB
|
754 lines
//• SpriteFlipNMask.c
//• Copyright (c) 1996 by Stefan C. Sinclair
//• All Rights Reserved Worldwide
//• Flips the PICTs & then creates the Mask PICTs
#include <EPPC.h>
#include <GestaltEqu.h>
#include <AppleEvents.h>
#include <Movies.h>
#include <ctype.h>
#include <Sound.h>
#include <Dialogs.h>
#define kGestaltMask 1L
#define kErrorAlertID 128
#define kMinTextPosition 0
#define kMaxTextPosition 32767
#define kFirstLifePICT 128
#define kNumLifePICT 43
#define kFirstProjectilePICT 214
#define kNumProjectilePICT 2
#define kFirstDeathPICT 218
#define kNumDeathPICT 10
#define kRideChairPICT 238
#define kSitChairPICT 240
#define kMaskOffset 200
void InitToolbox(void);
void AEInstallHandlers(void);
pascal OSErr DoOpenApp( AppleEvent *event, AppleEvent *reply, long refcon);
pascal OSErr DoOpenDoc( AppleEvent *event, AppleEvent *reply, long refcon);
pascal OSErr DoPrintDoc( AppleEvent *event, AppleEvent *reply, long refcon);
pascal OSErr DoQuitApp( AppleEvent *event, AppleEvent *reply, long refcon);
OSErr CheckForRequiredParams( AppleEvent *event);
void AEInit(void);
void MoviesInit( void );
void Open_A_Movie( void );
void Close_A_Movie( void );
void DoError (char desc[255], short OScode, short myRef, Boolean fatal);
void pStrCopy (StringPtr p1, StringPtr p2);
pascal Boolean SpriteGuyFileFilter(fileParam *thePB );
void Handle_One_Event( void );
Boolean All_Done = FALSE;
EventRecord The_Event;
main()
{
InitToolbox();
MoviesInit();
AEInit();
while ( All_Done == false )
Handle_One_Event();
DoError("Ha! No errors! Apparently, it worked.", 0, 0, FALSE);
return 0;
}
void Handle_One_Event( void )
{
WindowPtr which_window;
long movie_related_event;
WaitNextEvent(everyEvent, &The_Event, 0, nil);
switch (The_Event.what)
{
case keyDown:
case autoKey:
break;
case updateEvt:
break;
case mouseDown:
Open_A_Movie();
break;
case kHighLevelEvent:
AEProcessAppleEvent(&The_Event);
break;
}
}
void InitToolbox()
{
InitGraf((Ptr) &qd.thePort);
InitFonts();
InitWindows();
InitMenus();
FlushEvents(everyEvent,0);
TEInit();
InitDialogs(0L);
InitCursor();
}
void MoviesInit( void )
{
OSErr error;
long result;
error = Gestalt( gestaltQuickTime, &result );
if ( error != noErr )
DoError("Sorry, but you need QuickTime™ installed for this program to work.",0,0,TRUE);
error = EnterMovies();
if ( error != noErr )
DoError("Sorry, but you need QuickTime™ installed for this program to work.",0,0,TRUE);
}
void AEInit( void)
{
OSErr err;
long feature;
err = Gestalt(gestaltAppleEventsAttr, &feature);
if(err != noErr)
DoError("Error returned by gestalt!",0,0, true);
if(!(feature & (kGestaltMask << gestaltAppleEventsPresent)))
DoError("Apple Events not supported!",0,0, true);
AEInstallHandlers();
}
void AEInstallHandlers(void)
{
OSErr err;
/* install the required apple event handlers */
AEEventHandlerUPP OPENae, QUITae, STARTae, PRINTae;
/* must use these for PPC proc pointers */
OPENae = NewAEEventHandlerProc ((ProcPtr) &DoOpenDoc);
QUITae = NewAEEventHandlerProc ((ProcPtr) &DoQuitApp);
STARTae = NewAEEventHandlerProc ((ProcPtr) &DoOpenApp);
PRINTae = NewAEEventHandlerProc ((ProcPtr) &DoPrintDoc);
err = AEInstallEventHandler( kCoreEventClass, kAEOpenApplication,STARTae, 0L, false);
if(err != noErr)
DoError("Error installing 'open app' handler!",0,0, true);
err = err = AEInstallEventHandler( kCoreEventClass, kAEOpenDocuments, OPENae, 0L, false);
if(err != noErr)
DoError("Error installing 'open doc' handler!",0,0, true);
err = err = AEInstallEventHandler( kCoreEventClass, kAEPrintDocuments, PRINTae, 0L, false);
if(err != noErr)
DoError("Error installing 'print doc' handler!",0,0, true);
err = err = AEInstallEventHandler( kCoreEventClass, kAEQuitApplication, QUITae, 0L, false);
if(err != noErr)
DoError("Error installing 'quit app' handler!",0,0, true);
}
pascal OSErr DoOpenApp( AppleEvent *event, AppleEvent *reply, long refcon)
{
Open_A_Movie();
return noErr;
}
pascal OSErr DoOpenDoc( AppleEvent *event, AppleEvent *reply, long refcon)
{
OSErr err;
FSSpec fileSpec;
long i, numDocs;
DescType returnedType;
AEKeyword keywd;
Size actualSize;
AEDescList docList = {typeNull, nil}; // Get the direct parameter-a descriptor list-and put into docList
err = AEGetParamDesc(event, keyDirectObject, typeAEList, &docList);
err = CheckForRequiredParams( event); // Check for missing required parameters
if(err)
{
err = AEDisposeDesc( &docList);
return err;
}
err = AECountItems( &docList, &numDocs); // Count #of descriptor records in the list
if(err) // Should be at least 1 since we got called & no error.
{
err = AEDisposeDesc( &docList);
return err;
}
// Now get each descriptor record, coerce data to an FSSpec record, & open the file
//for( i = 1; i <= numDocs; i++)
//{
err = AEGetNthPtr( &docList, 1, typeFSS, &keywd, &returnedType, (Ptr)&fileSpec, sizeof(fileSpec), &actualSize);
Open_A_Movie();
//}
err = AEDisposeDesc( &docList);
return err;
}
OSErr CheckForRequiredParams( AppleEvent *appleEventPtr)
{
DescType returnedType;
Size actualSize;
OSErr err;
err = AEGetAttributePtr( appleEventPtr, keyMissedKeywordAttr, typeWildCard, &returnedType, nil, 0, &actualSize);
//• Looks for specified attribute, returns attribute status as an error
if( err == errAEDescNotFound)
return noErr;
else if( err == noErr)
return errAEParamMissed;
else
return err;
}
pascal OSErr DoPrintDoc( AppleEvent *event, AppleEvent *reply, long refcon)
{
return noErr;
}
pascal OSErr DoQuitApp( AppleEvent *event, AppleEvent *reply, long refcon)
{
All_Done = true;
return noErr;
}
void Open_A_Movie( void )
{
SFTypeList type_list = { MovieFileType,'MYqt', 0, 0 };
StandardFileReply the_reply;
Str255 movie_name;
Boolean was_changed;
FileFilterUPP myFileFilter;
short curResRefNum, i, j, k, x, y, high, wide;
Rect rWind, rPICT, rPICT2;
CWindowPtr aWindow;
OSErr err;
BitMap bmp;
PicHandle spritePICT, flipPICT, maskPICT, flipMaskPICT;
Rect r;
RGBColor c, b = {0,0,0}, w = {65535, 65535, 65535};
myFileFilter = NewFileFilterProc(SpriteGuyFileFilter);
StandardGetFilePreview( myFileFilter, -1/*numTypes*/, type_list, &the_reply );
if( the_reply.sfGood != true )
ExitToShell();
else
{
curResRefNum = HOpenResFile(the_reply.sfFile.vRefNum,
the_reply.sfFile.parID,the_reply.sfFile.name,
fsRdWrPerm);
err = ((ResError()!=noErr) || (curResRefNum < 0));
if (err != noErr)
DoError("Error opening resource fork!",err,0,TRUE);
else
{
rWind.top = rWind.left = 60;
rWind.bottom = 300;
rWind.right = 600;
aWindow = (CWindowPtr)NewCWindow(nil, &rWind, "\pFlip 'n Mask",
TRUE, documentProc, (WindowPtr)-1L, TRUE, 0);
SetPort((GrafPtr)aWindow);
ShowWindow((WindowPtr)aWindow);
ValidRect(&aWindow->portRect);
UseResFile(curResRefNum);
ForeColor(blackColor);
BackColor(whiteColor);
for(i=kFirstLifePICT; i<(kFirstLifePICT+kNumLifePICT); i++)
{
ClipRect(&qd.thePort->portRect);
EraseRect(&qd.thePort->portRect);
spritePICT = (PicHandle)Get1Resource('PICT', i);
if(spritePICT == NULL)
DoError("File is missing a PICT resource!",i,0,TRUE);
HLock((Handle)spritePICT);
rPICT = (**spritePICT).picFrame;
rPICT2 = rPICT;
high = rPICT.bottom - rPICT.top;
wide = rPICT.right - rPICT.left;
DrawPicture(spritePICT, &rPICT);
rPICT2.left+=(wide+1);
rPICT2.right+=(wide+1);
// flip it
for(j=0; j<wide; j++)
{
for(k=0; k<high; k++)
{
GetCPixel(j, k, &c);
x = rPICT2.right - j;
SetCPixel(x, k, &c);
}
}
flipPICT = OpenPicture(&rPICT2);
CopyBits(&qd.thePort->portBits,&qd.thePort->portBits,
&rPICT2, &rPICT2, srcCopy, nil);
ClosePicture();
AddResource((Handle)flipPICT, 'PICT', (i+kNumLifePICT), "\p");
if(ResError())
{
DoError("ResError!",i,ResError(),TRUE);
}
WriteResource((Handle)flipPICT);
// mask the original
for(j=0; j<wide; j++)
{
for(k=0; k<high; k++)
{
GetCPixel(j, k, &c);
if(c.red == w.red && c.blue == w.blue && c.green == w.green)
;
else
SetCPixel(j, k, &b); // not white, so make it black
}
}
maskPICT = OpenPicture(&rPICT);
CopyBits(&qd.thePort->portBits,&qd.thePort->portBits,
&rPICT, &rPICT, srcCopy, nil);
ClosePicture();
AddResource((Handle)maskPICT, 'PICT', (i+kMaskOffset), "\p");
if(ResError())
{
DoError("ResError!",i,ResError(),TRUE);
}
WriteResource((Handle)maskPICT);
// flip the mask now
for(j=0; j<wide; j++)
{
for(k=0; k<high; k++)
{
GetCPixel(j, k, &c);
x = rPICT2.right - j;
SetCPixel(x, k, &c);
}
}
flipMaskPICT = OpenPicture(&rPICT2);
CopyBits(&qd.thePort->portBits,&qd.thePort->portBits,
&rPICT2, &rPICT2, srcCopy, nil);
ClosePicture();
AddResource((Handle)flipMaskPICT, 'PICT', (i+kNumLifePICT+kMaskOffset), "\p");
if(ResError())
{
DoError("ResError!",i,ResError(),TRUE);
}
WriteResource((Handle)flipMaskPICT);
// dispose of all PICTs
ReleaseResource((Handle)spritePICT);
ReleaseResource((Handle)maskPICT);
ReleaseResource((Handle)flipMaskPICT);
ReleaseResource((Handle)flipPICT);
EraseRect(&qd.thePort->portRect);
}
for(i=kFirstProjectilePICT; i<(kFirstProjectilePICT+kNumProjectilePICT); i++)
{
ClipRect(&qd.thePort->portRect);
EraseRect(&qd.thePort->portRect);
spritePICT = (PicHandle)Get1Resource('PICT', i);
if(spritePICT == NULL)
DoError("File is missing a PICT resource!",i,0,TRUE);
HLock((Handle)spritePICT);
rPICT = (**spritePICT).picFrame;
rPICT2 = rPICT;
high = rPICT.bottom - rPICT.top;
wide = rPICT.right - rPICT.left;
DrawPicture(spritePICT, &rPICT);
rPICT2.left+=(wide+1);
rPICT2.right+=(wide+1);
DrawPicture(spritePICT, &rPICT);
// flip it
for(j=0; j<wide; j++)
{
for(k=0; k<high; k++)
{
GetCPixel(j, k, &c);
x = rPICT2.right - j;
SetCPixel(x, k, &c);
}
}
flipPICT = OpenPicture(&rPICT2);
CopyBits(&qd.thePort->portBits,&qd.thePort->portBits,
&rPICT2, &rPICT2, srcCopy, nil);
ClosePicture();
AddResource((Handle)flipPICT, 'PICT', (i+kNumProjectilePICT), "\p");
if(ResError())
{
DoError("ResError!",i,ResError(),TRUE);
}
WriteResource((Handle)flipPICT);
// mask the original
for(j=0; j<wide; j++)
{
for(k=0; k<high; k++)
{
GetCPixel(j, k, &c);
if(c.red == w.red && c.blue == w.blue && c.green == w.green)
;
else
SetCPixel(j, k, &b); // not white, so make it black
}
}
maskPICT = OpenPicture(&rPICT);
CopyBits(&qd.thePort->portBits,&qd.thePort->portBits,
&rPICT, &rPICT, srcCopy, nil);
ClosePicture();
AddResource((Handle)maskPICT, 'PICT', (i+kMaskOffset), "\p");
if(ResError())
{
DoError("ResError!",i,ResError(),TRUE);
}
WriteResource((Handle)maskPICT);
// flip the mask now
for(j=0; j<wide; j++)
{
for(k=0; k<high; k++)
{
GetCPixel(j, k, &c);
x = rPICT2.right - j;
SetCPixel(x, k, &c);
}
}
flipMaskPICT = OpenPicture(&rPICT2);
CopyBits(&qd.thePort->portBits,&qd.thePort->portBits,
&rPICT2, &rPICT2, srcCopy, nil);
ClosePicture();
AddResource((Handle)flipMaskPICT, 'PICT', (i+kNumProjectilePICT+kMaskOffset), "\p");
if(ResError())
{
DoError("ResError!",i,ResError(),TRUE);
}
WriteResource((Handle)flipMaskPICT);
// dispose of all PICTs
ReleaseResource((Handle)spritePICT);
ReleaseResource((Handle)maskPICT);
ReleaseResource((Handle)flipMaskPICT);
ReleaseResource((Handle)flipPICT);
EraseRect(&qd.thePort->portRect);
}
for(i=kFirstDeathPICT; i<(kFirstDeathPICT+kNumDeathPICT); i++)
{
ClipRect(&qd.thePort->portRect);
EraseRect(&qd.thePort->portRect);
spritePICT = (PicHandle)Get1Resource('PICT', i);
if(spritePICT == NULL)
DoError("File is missing a PICT resource!",i,0,TRUE);
HLock((Handle)spritePICT);
rPICT = (**spritePICT).picFrame;
rPICT2 = rPICT;
high = rPICT.bottom - rPICT.top;
wide = rPICT.right - rPICT.left;
DrawPicture(spritePICT, &rPICT);
rPICT2.left+=(wide+1);
rPICT2.right+=(wide+1);
DrawPicture(spritePICT, &rPICT);
// flip it
for(j=0; j<wide; j++)
{
for(k=0; k<high; k++)
{
GetCPixel(j, k, &c);
x = rPICT2.right - j;
SetCPixel(x, k, &c);
}
}
flipPICT = OpenPicture(&rPICT2);
CopyBits(&qd.thePort->portBits,&qd.thePort->portBits,
&rPICT2, &rPICT2, srcCopy, nil);
ClosePicture();
AddResource((Handle)flipPICT, 'PICT', (i+kNumDeathPICT), "\p");
if(ResError())
{
DoError("ResError!",i,ResError(),TRUE);
}
WriteResource((Handle)flipPICT);
// mask the original
for(j=0; j<wide; j++)
{
for(k=0; k<high; k++)
{
GetCPixel(j, k, &c);
if(c.red == w.red && c.blue == w.blue && c.green == w.green)
;
else
SetCPixel(j, k, &b); // not white, so make it black
}
}
maskPICT = OpenPicture(&rPICT);
CopyBits(&qd.thePort->portBits,&qd.thePort->portBits,
&rPICT, &rPICT, srcCopy, nil);
ClosePicture();
AddResource((Handle)maskPICT, 'PICT', (i+kMaskOffset), "\p");
if(ResError())
{
DoError("ResError!",i,ResError(),TRUE);
}
WriteResource((Handle)maskPICT);
// flip the mask now
for(j=0; j<wide; j++)
{
for(k=0; k<high; k++)
{
GetCPixel(j, k, &c);
x = rPICT2.right - j;
SetCPixel(x, k, &c);
}
}
flipMaskPICT = OpenPicture(&rPICT2);
CopyBits(&qd.thePort->portBits,&qd.thePort->portBits,
&rPICT2, &rPICT2, srcCopy, nil);
ClosePicture();
AddResource((Handle)flipMaskPICT, 'PICT', (i+kNumDeathPICT+kMaskOffset), "\p");
if(ResError())
{
DoError("ResError!",i,ResError(),TRUE);
}
WriteResource((Handle)flipMaskPICT);
// dispose of all PICTs
ReleaseResource((Handle)spritePICT);
ReleaseResource((Handle)maskPICT);
ReleaseResource((Handle)flipMaskPICT);
ReleaseResource((Handle)flipPICT);
EraseRect(&qd.thePort->portRect);
}
for(i=kRideChairPICT; i<(kRideChairPICT+1); i++)
{
ClipRect(&qd.thePort->portRect);
EraseRect(&qd.thePort->portRect);
spritePICT = (PicHandle)Get1Resource('PICT', i);
if(spritePICT == NULL)
DoError("File is missing a PICT resource!",i,0,TRUE);
HLock((Handle)spritePICT);
rPICT = (**spritePICT).picFrame;
rPICT2 = rPICT;
high = rPICT.bottom - rPICT.top;
wide = rPICT.right - rPICT.left;
DrawPicture(spritePICT, &rPICT);
rPICT2.left+=(wide+1);
rPICT2.right+=(wide+1);
DrawPicture(spritePICT, &rPICT);
// flip it
for(j=0; j<wide; j++)
{
for(k=0; k<high; k++)
{
GetCPixel(j, k, &c);
x = rPICT2.right - j;
SetCPixel(x, k, &c);
}
}
flipPICT = OpenPicture(&rPICT2);
CopyBits(&qd.thePort->portBits,&qd.thePort->portBits,
&rPICT2, &rPICT2, srcCopy, nil);
ClosePicture();
AddResource((Handle)flipPICT, 'PICT', (i+1), "\p");
if(ResError())
{
DoError("ResError!",i,ResError(),TRUE);
}
WriteResource((Handle)flipPICT);
// mask the original
for(j=0; j<wide; j++)
{
for(k=0; k<high; k++)
{
GetCPixel(j, k, &c);
if(c.red == w.red && c.blue == w.blue && c.green == w.green)
;
else
SetCPixel(j, k, &b); // not white, so make it black
}
}
maskPICT = OpenPicture(&rPICT);
CopyBits(&qd.thePort->portBits,&qd.thePort->portBits,
&rPICT, &rPICT, srcCopy, nil);
ClosePicture();
AddResource((Handle)maskPICT, 'PICT', (i+kMaskOffset), "\p");
if(ResError())
{
DoError("ResError!",i,ResError(),TRUE);
}
WriteResource((Handle)maskPICT);
// flip the mask now
for(j=0; j<wide; j++)
{
for(k=0; k<high; k++)
{
GetCPixel(j, k, &c);
x = rPICT2.right - j;
SetCPixel(x, k, &c);
}
}
flipMaskPICT = OpenPicture(&rPICT2);
CopyBits(&qd.thePort->portBits,&qd.thePort->portBits,
&rPICT2, &rPICT2, srcCopy, nil);
ClosePicture();
AddResource((Handle)flipMaskPICT, 'PICT', (i+1+kMaskOffset), "\p");
if(ResError())
{
DoError("ResError!",i,ResError(),TRUE);
}
WriteResource((Handle)flipMaskPICT);
// dispose of all PICTs
ReleaseResource((Handle)spritePICT);
ReleaseResource((Handle)maskPICT);
ReleaseResource((Handle)flipMaskPICT);
ReleaseResource((Handle)flipPICT);
EraseRect(&qd.thePort->portRect);
}
for(i=kSitChairPICT; i<(kSitChairPICT+1); i++)
{
ClipRect(&qd.thePort->portRect);
EraseRect(&qd.thePort->portRect);
spritePICT = (PicHandle)Get1Resource('PICT', i);
if(spritePICT == NULL)
DoError("File is missing a PICT resource!",i,0,TRUE);
HLock((Handle)spritePICT);
rPICT = (**spritePICT).picFrame;
rPICT2 = rPICT;
high = rPICT.bottom - rPICT.top;
wide = rPICT.right - rPICT.left;
DrawPicture(spritePICT, &rPICT);
rPICT2.left+=(wide+1);
rPICT2.right+=(wide+1);
DrawPicture(spritePICT, &rPICT);
// flip it
for(j=0; j<wide; j++)
{
for(k=0; k<high; k++)
{
GetCPixel(j, k, &c);
x = rPICT2.right - j;
SetCPixel(x, k, &c);
}
}
flipPICT = OpenPicture(&rPICT2);
CopyBits(&qd.thePort->portBits,&qd.thePort->portBits,
&rPICT2, &rPICT2, srcCopy, nil);
ClosePicture();
AddResource((Handle)flipPICT, 'PICT', (i+1), "\p");
if(ResError())
{
DoError("ResError!",i,ResError(),TRUE);
}
WriteResource((Handle)flipPICT);
// mask the original
for(j=0; j<wide; j++)
{
for(k=0; k<high; k++)
{
GetCPixel(j, k, &c);
if(c.red == w.red && c.blue == w.blue && c.green == w.green)
;
else
SetCPixel(j, k, &b); // not white, so make it black
}
}
maskPICT = OpenPicture(&rPICT);
CopyBits(&qd.thePort->portBits,&qd.thePort->portBits,
&rPICT, &rPICT, srcCopy, nil);
ClosePicture();
AddResource((Handle)maskPICT, 'PICT', (i+kMaskOffset), "\p");
if(ResError())
{
DoError("ResError!",i,ResError(),TRUE);
}
WriteResource((Handle)maskPICT);
// flip the mask now
for(j=0; j<wide; j++)
{
for(k=0; k<high; k++)
{
GetCPixel(j, k, &c);
x = rPICT2.right - j;
SetCPixel(x, k, &c);
}
}
flipMaskPICT = OpenPicture(&rPICT2);
CopyBits(&qd.thePort->portBits,&qd.thePort->portBits,
&rPICT2, &rPICT2, srcCopy, nil);
ClosePicture();
AddResource((Handle)flipMaskPICT, 'PICT', (i+1+kMaskOffset), "\p");
if(ResError())
{
DoError("ResError!",i,ResError(),TRUE);
}
WriteResource((Handle)flipMaskPICT);
// dispose of all PICTs
ReleaseResource((Handle)spritePICT);
ReleaseResource((Handle)maskPICT);
ReleaseResource((Handle)flipMaskPICT);
ReleaseResource((Handle)flipPICT);
EraseRect(&qd.thePort->portRect);
}
CloseResFile(curResRefNum);
All_Done = TRUE;
}
}
All_Done = TRUE;
}
//************************************************************************
void DoError (char desc[255], short OScode, short myRef, Boolean fatal)
{
/* A fatal error has occurred - report it and quit */
short go;
Str63 a, b;
c2pstr(desc);
NumToString (OScode, a);
NumToString (myRef, b);
ParamText ((StringPtr)desc, a, b, NULL);
NoteAlert(kErrorAlertID,NULL);
if(fatal)
ExitToShell();
}
/* This filters out (discards) non-sprite files.*/
pascal Boolean SpriteGuyFileFilter(fileParam *thePB )
{
if (thePB->ioFlFndrInfo.fdType=='Ned3')
return(FALSE); /* SF2K3 file, keep it in the list */
else if ( thePB->ioFlFndrInfo.fdType == 'Ned2' )
return(FALSE); /* SF2K2 file, keep it in the list */
else
return(TRUE); /* Nope, filter it from the list */
}